<!--购物车-->
<template>
  <div class="cart-page">
    <!-- 空购物车的内容 -->
    <div class="emptycart" v-if="!showcart">
      <img src="https://x.dscmall.cn/static/dist/img/cart-empty.png" alt="" />
      <p>购物车空空,快去逛逛把</p>
      <button>去逛逛</button>
      <button>去看看</button>
    </div>

    <div class="cart-list" v-else>
      <div class="address">
        <span>江苏省 无锡市 滨湖区 蠡园街道</span>
      </div>
      <div class="cart-content">
        <ul class="cart-shop">
          <li
            v-for="(cartListData, index) in cartListDatas"
            :key="cartListData.ru_id"
          >
            <!-- 店铺的信息 -->
            <div class="shop-name">
              <div
                class="shop-select"
                @click="shopSelectFun(cartListData, index)"
              >
                <!-- 选中的 -->
                <i
                  class="iconfont icon-yuanxingxuanzhongfill"
                  v-if="cartListData.isShopSelect"
                ></i>
                <!-- 未选中的 -->
                <i class="iconfont icon-yuanxingweixuanzhong" v-else></i>
              </div>
              <h3>{{ cartListData.rz_shop_name }}</h3>
            </div>
            <!-- 所有商品的信息 -->
            <ul class="cart-goods-list">
              <li
                v-for="(cartgoodsitem, index) in cartListData.children"
                :key="cartgoodsitem.goods_id"
              >
                <div
                  class="goods-select"
                  @click="goodsSelectFun(cartgoodsitem, index)"
                >
                  <!-- 选中的 -->
                  <i
                    class="iconfont icon-yuanxingxuanzhongfill"
                    v-if="cartgoodsitem.isSelect"
                  ></i>
                  <!-- 未选中的 -->
                  <i class="iconfont icon-yuanxingweixuanzhong" v-else></i>
                </div>
                <div class="goods-content">
                  <img :src="cartgoodsitem.goods_thumb" alt="" />
                  <div class="goods-info">
                    <div
                      class="goods-title"
                      @click="toDetails(cartgoodsitem.goods_id)"
                    >
                      {{ cartgoodsitem.goods_name }}
                    </div>
                    <div class="goods-wrap">
                      <div class="goods-price">
                        ¥{{ cartgoodsitem.shop_price }}
                      </div>
                      <!-- 操作相关的内容 -->
                      <div class="addorminus">
                        <button
                          class="minus"
                          @click="minusCartNumFun(cartgoodsitem, index)"
                        >
                          -
                        </button>
                        <input
                          type="text"
                          v-model="cartgoodsitem.buyNum"
                          class="numtext"
                        />
                        <button
                          class="add"
                          @click="addCartNumFun(cartgoodsitem, index)"
                        >
                          +
                        </button>
                      </div>
                      <i class="iconfont icon-shoucang1"></i>
                      <i
                        class="iconfont icon-shanchu1"
                        @click="deleteCartFun(cartgoodsitem, index)"
                      ></i>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-footer">
      <div class="select">
        <div class="selectall" @click="selectAllFun">
          <!-- 选中的 -->
          <i class="iconfont icon-yuanxingxuanzhongfill" v-if="isSelectAll"></i>
          <!-- 未选中的 -->
          <i class="iconfont icon-yuanxingweixuanzhong" v-else></i>
        </div>
        <span>全选</span>
      </div>
      <!-- 合计的内容 -->
      <div class="total">
        <span>合计:</span>
        <span class="total-price">{{ totalPrice }}</span>
        <p>(以节省多少钱)</p>
      </div>
      <mt-button type="danger">去结算({{ totalNum }})</mt-button>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      // showcart: true,
      // cartListDatas: [],
      // buyNum: 1,
      // isSelectAll: true,
    };
  },
  //计算属性 类似于data概念
  computed: {
    // 获取购物车的数据
    cartListDatas() {
      return this.$store.state.cartListDatas;
    },
    // 购物车的元素是否显示
    showcart() {
      if (this.$store.state.cartListDatas.length > 0) {
        return true;
      } else {
        return false;
      }
    },
    isSelectAll() {
      return this.$store.state.isSelectAll;
    },
    totalPrice() {
      let price = 0;
      // 取出来购物车的数据
      let cartListDatas = this.$store.state.cartListDatas;
      cartListDatas.map((item) => {
        item.children.map((value) => {
          if (value.isSelect) {
            price += value.shop_price * value.buyNum;
          }
        });
      });
      return price;
    },
    totalNum() {
      let num = 0;
      // 取出来购物车的数据
      let cartListDatas = this.$store.state.cartListDatas;
      cartListDatas.map((item) => {
        item.children.map((value) => {
          if (value.isSelect) {
            num += value.buyNum;
          }
        });
      });
      return num;
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 减操作
    minusCartNumFun(cartgoodsitem, index) {
      if (cartgoodsitem.buyNum > 1) {
        cartgoodsitem.buyNum -= 1;
      } else {
        cartgoodsitem.buyNum = 1;
      }
      let data = {
        cartgoodsitem,
        index,
      };
      this.$store.commit("minusCartNum", data);
    },
    // 加操作
    addCartNumFun(cartgoodsitem, index) {
      // 加1后提交数据的更新
      cartgoodsitem.buyNum += 1;
      let data = {
        cartgoodsitem,
        index,
      };
      this.$store.commit("addCartNum", data);
    },
    // 跳转到详情页
    toDetails(goods_id) {
      this.$router.push("/details/" + goods_id);
    },
    // 当条商品的选择
    goodsSelectFun(cartgoodsitem, index) {
      // 更新视图的选择
      cartgoodsitem.isSelect = !cartgoodsitem.isSelect;
      let data = {
        cartgoodsitem,
        index,
      };
      this.$store.commit("goodsSelect", data);
    },
    // 店铺选择的事件
    shopSelectFun(cartListData, index) {
      // 店铺选择取反
      cartListData.isShopSelect = !cartListData.isShopSelect;
      let data = {
        cartListData,
        index,
      };
      this.$store.commit("shopSelect", data);
    },
    // 全选
    selectAllFun() {
      this.$store.commit("selectAll");
    },
    // 点击删除按钮后
    deleteCartFun(cartgoodsitem, index) {
      let data = {
        cartgoodsitem,
        index,
      };
      this.$store.commit("deleteCart", data);
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    console.log(this.$store.state.cartListDatas);
  },
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less">
.cart-page {
  i {
    font-size: 2rem;
  }
  .icon-yuanxingxuanzhongfill {
    color: rgb(242, 14, 40);
  }
  .emptycart {
    margin: 1.1rem;
    background-color: #fff;
    border-radius: 0.5rem;
    overflow: hidden;
    text-align: center;
    padding-bottom: 4rem;
  }
  .cart-list {
    .address {
      height: 5rem;
      line-height: 5rem;
      padding-left: 1rem;
    }
    .cart-content {
      .cart-shop {
        > li {
          width: calc(100% - 2rem);
          margin: 1rem;
          background: #fff;
          border-radius: 1rem;
          .shop-name {
            width: calc(100% - 2rem);
            margin: 0 1rem;
            height: 4.4rem;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            font-size: 1.4rem;
            margin-left: 1rem;
            // background: pink;
            h3 {
              margin-left: 2rem;
            }
          }
          .cart-goods-list {
            li {
              height: 8rem;
              width: calc(100% - 2rem);
              margin: 0rem 1rem;
              display: flex;
              align-items: center;
              .goods-content {
                display: flex;
                img {
                  width: 7rem;
                  height: 7rem;
                }
                .goods-info {
                  .goods-title {
                    width: 23.3rem;
                    height: 4rem;
                    line-height: 2rem;
                    font-size: 1.3rem;
                    //单行文本出现省略号
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                  }
                  .goods-wrap {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    height: 3rem;
                    // background: pink;
                    .goods-price {
                      font-size: 1.5rem;
                      color: rgb(242, 14, 40);
                      font-weight: 700;
                    }
                    .addorminus {
                      display: flex;
                      justify-content: space-between;
                      align-items: center;
                      padding: 1rem;
                      margin-left: 3rem;
                      button {
                        width: 2rem;
                        height: 2rem;
                        border-radius: 0 5px 5px 0;
                        background-color: #f2f3f5;
                      }
                      .minus {
                        border-radius: 5px 0 0 5px;
                      }
                      .add {
                        border-radius: 0 5px 5px 0;
                      }
                      input {
                        width: 2.5rem;
                        height: 2rem;
                        padding: 0;
                        font-size: 1.2rem;
                        border: 0;
                        margin: 1px;
                        background-color: #f2f3f5;
                        border-width: 1px 0;
                        color: #323233;
                        text-align: center;
                      }
                    }
                    i {
                      margin-right: 0.3rem;
                      color: #666;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  .cart-footer {
    width: calc(100% - 2rem);
    padding: 0 1rem;
    height: 4.4rem;
    position: fixed;
    bottom: 4.9rem;
    left: 0;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1.4rem;
    .select {
      display: flex;
      .selectall {
        span {
          margin-left: 1rem;
        }
      }
    }

    .total {
      flex: 1;
      font-weight: 500;
      text-align: right;
      color: #333;
      padding-right: 1.2rem;
      .total-price {
        font-size: 1.8rem;
        font-weight: 700;
        color: rgb(242, 14, 40);
        margin-left: 1.2rem;
      }
      p {
        font-size: 1.2rem;
        color: #999;
      }
    }
  }
}
</style>